package com.ss.web.tagmark.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ss.web.tagmark.entity.TagInfo;
import com.ss.web.tagmark.entity.User;
import com.ss.web.tagmark.service.ParameterService;
import com.ss.web.tagmark.service.TagService;
import com.ss.web.tagmark.service.UserService;

/**
 * 用户管理相关的操作
 *
 * @author taofucheng
 *
 */
@Controller
@RequestMapping("/users")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private ParameterService paramService;
	@Autowired
	private TagService tagService;

	/**
	 * 用户管理的首页
	 *
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(@ModelAttribute("user") User user, HttpServletRequest request, Model model) throws IOException {
		Page<User> users = userService.getListByPage(request.getParameterMap(), user);
		model.addAttribute("users", users);
		model.addAttribute("tags", user.getTags());
		model.addAttribute("status", appendAllOption(paramService.getUserStatus()));
		return "user/workspace";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(@ModelAttribute("user") User user, Model model) throws IOException {
		model.addAttribute("status", paramService.getUserStatus());
		return "user/add";
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String add(String newtags, @ModelAttribute("user") User user, Model model, HttpServletResponse response)
			throws IOException {
		try {
			userService.add(user, newtags);
			addFlashMsg(response, "添加成功！");
			return redirect("/users");
		} catch (Exception e) {
			model.addAttribute("newtags", newtags);
			model.addAttribute("tags", user.getTags());
			model.addAttribute("status", paramService.getUserStatus());
			model.addAttribute("msg", e.getMessage());
			return "user/add";
		}
	}

	@RequestMapping(value = "/{userId:\\d+}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable Long userId, Model model, HttpServletResponse response) throws IOException {
		User user = userService.getById(userId);
		if (user == null) {
			addFlashMsg(response, "指定的用户不存在，无法修改！");
			return redirect("/users");
		}
		model.addAttribute("user", user);
		model.addAttribute("tags", tagService.getTagsByUser(user.getUserId()));
		model.addAttribute("status", paramService.getUserStatus());
		return "user/modify";
	}

	@RequestMapping(value = "/{userId:\\d+}/edit", method = RequestMethod.PUT)
	public String edit(String newtags, @ModelAttribute("user") User user, Model model, HttpServletResponse response)
			throws IOException {
		try {
			userService.update(user, newtags);
			addFlashMsg(response, "修改成功！");
			return redirect("/users");
		} catch (Exception e) {
			model.addAttribute("newtags", newtags);
			model.addAttribute("tags", user.getTags());
			model.addAttribute("status", paramService.getUserStatus());
			model.addAttribute("msg", e.getMessage());
			return "user/modify";
		}
	}

	@RequestMapping(value = "/{userId:\\d+}/delete", method = RequestMethod.DELETE)
	@ResponseBody
	public boolean delete(@PathVariable Long userId, Model model, HttpServletResponse response) throws IOException {
		try {
			userService.deleteById(userId);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	/**
	 * 下拉框选择
	 *
	 * @param q
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	@ResponseBody
	public List<User> search(String q) throws IOException {
		return userService.find4Search(q);
	}

	/**
	 * 获取指定用户的所有标签信息
	 *
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/{userId:\\d+}/tags", method = RequestMethod.GET)
	@ResponseBody
	public List<TagInfo> tags(@PathVariable Long userId) throws IOException {
		return tagService.getTagsByUser(userId);
	}

	/**
	 * 获取指定用户的头像
	 *
	 * @param userId
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/{userId:\\d+}/heardimg.jpg", method = RequestMethod.GET)
	public void image(@PathVariable Long userId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		// TODO 获取用户的图像，并输出
		try {
			response.sendRedirect("/images/noheader.png");
		} catch (Exception e) {
		}
	}

	/**
	 * 获取用户状态的文本
	 *
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/statustext/{statusValue}", method = RequestMethod.GET)
	@ResponseBody
	public String statusText(@PathVariable String statusValue) throws IOException {
		try {
			return paramService.getByTypeAndValue(ParameterService.USER_STATUS, statusValue).getLabel();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return "";
		}
	}

}
